﻿var page = new lg.page({
    data: {
        Id: 0,
        Type: "",
    },
    mounted: function () {
        this.List();
    },
    methods: {
        List: function () {
            layui.use(['table', 'element'], function () {
                var table = layui.table;
                page.laytable = table.render({
                    elem: '#ADList'
                    , url: '/SysApi/GetADs'
                    , toolbar: '#toolbar' //开启头部工具栏，并为其绑定左侧模板
                    , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                    , cols: [[
                        {
                            field: 'SubCount', title: '#', width: 60, templet: function (d) {
                                return d.LAY_TABLE_INDEX + 1
                            }
                        }
                        , { field: 'Title', title: '广告位名称', width: 150 }
                        , {
                            field: 'Id', title: '标签', templet: function (d) {
                                return "&lttm:set name=\"AD" + d.Id + "\" value=\"{$FuncAD(" + d.Id + ")}\"&gt{$AD" + d.Id + "}";
                            }
                        }
                        , {
                            field: 'Type', title: '类型', width: 120, templet: function (d) {
                                return d.Type == 0 ? "自定义" : "图片轮播";
                            }
                        }
                        , {
                            field: 'IsUsing', title: '是否启用', width: 100, align: "center", templet: function (d) {
                                return laySwitch.render(d.IsUsing, 'use')
                            }
                        }
                        , { fixed: 'right', title: '操作', toolbar: '#btTpl', width: 160 }
                    ]],
                    page: true
                });
                //头工具栏事件
                table.on('toolbar(ADList)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch (obj.event) {
                        case 'addAD':
                            loadTpl({
                                url: "/Lgm/AddAD",
                                target: "#floatBody"
                            })
                            return false;
                            break;
                        case 'refresh':
                            page.laytable.reload();
                            break;
                    };
                });
                //监听行工具事件
                table.on('tool(ADList)', function (obj) {
                    var data = obj.data;
                    //console.log(obj)
                    switch (obj.event) {
                        case 'del':
                            layer.confirm('确定要删除吗?', function (index) {
                                lg.request({
                                    url: "/SysApi/DeleteAD",
                                    method: "post",
                                    data: { ids: data.Id },
                                    success: function (d) {
                                        if (d.code == 0) {
                                            layer.msg("删除成功!", { icon: 1 });
                                            page.laytable.reload();
                                        } else {
                                            layer.msg(d.msg, { icon: 2 })
                                        }
                                    }
                                })
                            });
                            break;
                        case 'edit':
                            loadTpl({
                                url: "/Lgm/EditAD/" + data.Id,
                                target: "#floatBody"
                            })
                            break;
                        case 'item':
                            top.location.href = "/Lgm/AdItem/" + data.Id;
                            break;
                        case 'use':
                            laySwitch.do({
                                obj: obj,
                                key: "IsUsing",
                                url: "/SysApi/EditAD"
                            })
                            break;
                    }

                });
            });
        },
        Add: function () {
            var form = layui.form;
            form.render();
            editor.render("editor");
            //下拉选择事件
            form.on('select(Type)', function (data) {
                if (data.value == 1) {
                    $("#EditorBox").hide()
                } else {
                    $("#EditorBox").show();
                }
            });
            form.on("submit(AddAD)", function (d) {
                if (!d.field.IsUsing) {
                    d.field.IsUsing = 0
                }
                lg.request({
                    url: "/SysApi/AddAD",
                    data: d.field,
                    method: 'post',
                    success: function (d) {
                        if (d.code == 0) {
                            layer.msg("添加成功！", { icon: 1 });
                            page.back(true);
                        } else {
                            layer.msg(d.msg, { icon: 2 })
                        }
                    }
                })
                return false;
            })
        },
        Edit: function (id) {
            var form = layui.form;
            form.render();
            lg.request({
                url: "/SysApi/GetAD/"+id,
                success: function (d) {
                    if (d.code == 0) {
                        if (typeof d.data == "object") {
                            form.val("ADEditForm", d.data)
                            editor.render("editor");
                        }
                    }
                }
            })
            form.on("submit(EditAD)", function (d) {
                if (!d.field.IsUsing) {
                    d.field.IsUsing = 0
                }
                lg.request({
                    url: "/SysApi/EditAD",
                    data: d.field,
                    method: 'post',
                    success: function (d) {
                        if (d.code == 0) {
                            layer.msg("添加成功！", { icon: 1 });
                            page.back(true);
                        } else {
                            layer.msg(d.msg, { icon: 2 })
                        }
                    }
                })
                return false;
            })
        },
        PicList: function () {
            var table = layui.table;
            page.laytable = table.render({
                elem: '#ADPicList'
                , url: '/SysApi/GetADs'
                , toolbar: '#toolbar' //开启头部工具栏，并为其绑定左侧模板
                , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                , cols: [[
                    {
                        field: 'SubCount', title: '#', width: 60, templet: function (d) {
                            return d.LAY_TABLE_INDEX + 1
                        }
                    }
                    , { field: 'ADTitle', title: '广告位名称', width: 150 }
                    , {
                        field: 'ID', title: '标签', templet: function (d) {
                            return "&lttm:set name=\"AD" + d.ID + "\" value=\"{$FuncAD(" + d.ID + ")}\"&gt{$AD" + d.ID + "}";
                        }
                    }
                    , {
                        field: 'ADType', title: '类型', width: 120, templet: function (d) {
                            return d.ADType == 0 ? "自定义" : "图片轮播";
                        }
                    }
                    , {
                        field: 'IsUsing', title: '是否启用', width: 100, align: "center", templet: function (d) {
                            return laySwitch.render(d.IsUsing, 'use')
                        }
                    }
                    , { fixed: 'right', title: '操作', toolbar: '#btTpl', width: 160 }
                ]],
            });
            //头工具栏事件
            table.on('toolbar(ADPicList)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'addADPic':
                        loadTpl({
                            url: "/Lgm/AddAD",
                            target: "#floatBody"
                        })
                        return false;
                        break;
                    case 'refresh':
                        page.laytable.reload();
                        break;
                };
            });
            //监听行工具事件
            table.on('tool(ADPicList)', function (obj) {
                var data = obj.data;
                switch (obj.event) {
                    case 'del':                    
                            lg.request({
                                url: "/SysApi/DeleteAD",
                                method: "post",
                                data: { ids: data.Id },
                                success: function (d) {
                                    if (d.code == 0) {
                                        layer.msg("删除成功!", { icon: 1 });
                                        page.laytable.reload();
                                    } else {
                                        layer.msg(d.msg, { icon: 2 })
                                    }
                                }
                        });
                        break;
                    case 'edit':
                        loadTpl({
                            url: "/Lgm/EditAD/" + data.Id,
                            target: "#floatBody"
                        })
                        break;
                    case 'use':
                        laySwitch.do({
                            obj: obj,
                            key: "IsUsing",
                            url: "/SysApi/EditAD",
                            refresh: true
                        })
                        break;
                }

            });
        }
    }
})